/******************************************************************************
 * Copyright (C) 2017 Chengdu Dlhd information industry co. LTD.
 * All Rights Reserved.
 * 本软件为成都顶联互动信息产业有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package net.dgg.dggcloud.service.jrcrm.service.cms;

import net.dgg.dggcloud.core.cache.CacheManager;
import net.dgg.dggcloud.core.idwork.KeyWorker;
import net.dgg.dggcloud.core.util.StringUtils;
import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.common.exception.BizException;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.jrcrm.constant.CommonConstant;
import net.dgg.dggcloud.service.jrcrm.dao.cms.TreeBookDao;
import net.dgg.dggcloud.service.jrcrm.entity.cms.TreeBook;
import net.dgg.dggcloud.service.jrcrm.entity.cms.TypeCost;
import org.apache.commons.lang.math.NumberUtils;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import javax.annotation.Resource;
import java.util.*;

/**
 * @ClassName: TypeCostService
 * @Description: 渠道成本
 * @Author 罗金
 * @date 2017/11/3 10:22
 */
@Service
public class TypeCostService extends BaseService {
    @Autowired
    protected KeyWorker keyWorker;
    @Resource
    private TreeBookService treeBookService;
    /**
     * 新增或者修改
     */
     public Long addNewCost(List<TypeCost> costList,User user,String typeCode){
         List<Map> listBook  = treeBookService.queryTreeBookListByCode(typeCode,2);
         for(Map map:listBook){
             TypeCost nTypeCost=null;
             for(TypeCost typeCost:costList) {
                 if (NumberUtils.isNumber(typeCost.getXdsmNotFormPrice()+"")|| NumberUtils.isNumber(typeCost.getXdsmFormPrice()+"")|| NumberUtils.isNumber(typeCost.getFormPrice()+"")||NumberUtils.isNumber(typeCost.getNotFormPrice()+"")) {
                     Query query = (Query) repository.getSession().createQuery("From TypeCost where typeCode ='" +typeCode + "" + "' and costMonth ='" + typeCost.getCostMonth() + "' and placeCode='" + typeCost.getPlaceCode() + "'");
                     if (query.list().size() > 1) {
                         nTypeCost = (TypeCost) query.list().get(0);
                         nTypeCost.setUpdaterUser(user);
                         nTypeCost.setPlaceCode(typeCost.getPlaceCode());
                         nTypeCost.setCostMonth(typeCost.getCostMonth());//月份
                         nTypeCost.setFormPrice(NumberUtils.toInt(typeCost.getFormPrice()+""));//表单价格
                         nTypeCost.setNotFormPrice(NumberUtils.toInt(typeCost.getNotFormPrice()+""));
                         nTypeCost.setXdsmFormPrice(NumberUtils.toInt(typeCost.getXdsmFormPrice()+""));
                         nTypeCost.setXdsmNotFormPrice(NumberUtils.toInt(typeCost.getXdsmNotFormPrice()+""));
                     } else {
                         nTypeCost = new TypeCost();
                         nTypeCost.setId(keyWorker.nextId());
                         nTypeCost.setCreateUser(user);
                         nTypeCost.setPlaceCode(typeCost.getPlaceCode());
                         nTypeCost.setCostMonth(typeCost.getCostMonth());//月份
                         nTypeCost.setFormPrice(NumberUtils.toInt(typeCost.getFormPrice()+""));//表单价格
                         nTypeCost.setNotFormPrice(NumberUtils.toInt(typeCost.getNotFormPrice()+""));
                         nTypeCost.setXdsmFormPrice(NumberUtils.toInt(typeCost.getXdsmFormPrice()+""));
                         nTypeCost.setXdsmNotFormPrice(NumberUtils.toInt(typeCost.getXdsmNotFormPrice()+""));
                     }
                     nTypeCost.setTypeCode(map.get("code")+"");
                     nTypeCost.setCostMonth(typeCost.getCostMonth());
                     repository.saveOrUpdate(nTypeCost);
                 }

             }
         }
         TypeCost nTypeCost=null;
         for(TypeCost typeCost:costList) {
             if (NumberUtils.isNumber(typeCost.getXdsmNotFormPrice()+"")|| NumberUtils.isNumber(typeCost.getXdsmFormPrice()+"")|| NumberUtils.isNumber(typeCost.getFormPrice()+"")||NumberUtils.isNumber(typeCost.getNotFormPrice()+"")) {
                 Query query = (Query) repository.getSession().createQuery("From TypeCost where typeCode ='" +typeCode + "" + "' and costMonth ='" + typeCost.getCostMonth() + "' and placeCode='" + typeCost.getPlaceCode() + "'");
                 if (query.list().size() > 1) {
                     nTypeCost = (TypeCost) query.list().get(0);
                     nTypeCost.setUpdaterUser(user);
                     nTypeCost.setPlaceCode(typeCost.getPlaceCode());
                     nTypeCost.setCostMonth(typeCost.getCostMonth());//月份
                     nTypeCost.setFormPrice(NumberUtils.toInt(typeCost.getFormPrice()+""));//表单价格
                     nTypeCost.setNotFormPrice(NumberUtils.toInt(typeCost.getNotFormPrice()+""));
                     nTypeCost.setXdsmFormPrice(NumberUtils.toInt(typeCost.getXdsmFormPrice()+""));
                     nTypeCost.setXdsmNotFormPrice(NumberUtils.toInt(typeCost.getXdsmNotFormPrice()+""));
                 } else {
                     nTypeCost = new TypeCost();
                     nTypeCost.setId(keyWorker.nextId());
                     nTypeCost.setCreateUser(user);
                     nTypeCost.setPlaceCode(typeCost.getPlaceCode());
                     nTypeCost.setCostMonth(typeCost.getCostMonth());//月份
                     nTypeCost.setFormPrice(NumberUtils.toInt(typeCost.getFormPrice()+""));//表单价格
                     nTypeCost.setNotFormPrice(NumberUtils.toInt(typeCost.getNotFormPrice()+""));
                     nTypeCost.setXdsmFormPrice(NumberUtils.toInt(typeCost.getXdsmFormPrice()+""));
                     nTypeCost.setXdsmNotFormPrice(NumberUtils.toInt(typeCost.getXdsmNotFormPrice()+""));
                 }
                 nTypeCost.setTypeCode(typeCode);
                 nTypeCost.setCostMonth(typeCost.getCostMonth());
                 repository.saveOrUpdate(nTypeCost);
             }

         }
         return  0L;
     }


     public List<TypeCost> getList(Map map){
         Query query = (Query) repository.getSession().createQuery("From TypeCost where typeCode ='" + map.get("code")+ "' and costMonth ='" + map.get("month") + "'");
     return query.list();
     }

    public TypeCost getTypeCost(Map map){
         StringBuffer str = new StringBuffer();
         if(map.get("code")!=null){
             str.append(" and typeCode ='" + map.get("code")+ "'");
         }
        if(map.get("month")!=null){
            str.append(" and costMonth ='" + map.get("month")+ "'");
        }
        if(map.get("place")!=null){
            str.append(" and placeCode ='" + map.get("place")+ "'");
        }
        Query query = (Query) repository.getSession().createQuery("From TypeCost where 1 = 1"+str);
        if(query.list().size()>0)
        return (TypeCost)query.list().get(0);
        return null;
    }


}
